global with sharing class SubmitUtil{
    static String testVar1 = '';


    Webservice static String createInquiryObject(String plandocumentId,String reason,String sub,String description,String approver){
        String str = null;
        String firstName = null;
        String lastName = null;
        Sales_Contract__c[] planDoc = null;
        
        DataAccessControlUtil dacu = new DataAccessControlUtil();
        SObject scObj = new Sales_Contract__c();
        SObject pdiObj = new Plan_Document_Inquiry__c();
        //Enters only if user has permission to update Sales_Contract__c and create Plan Document Inquiry object
        if(dacu.isUpdateableObjectByUser(scobj) == 'success' && dacu.isCreateableObjectByUser(pdiObj) == 'success')
        {
            String[] scExpectedFields = new String[]{'Id','Status__c', 'FirstName__c', 'LastName__c', 'Submit_Inquiry__c', 'User_ID__c'};
            Map<String, Schema.SObjectField> scFieldMap = dacu.getFieldMap(scObj);
            Set<String> allowedROFields = dacu.getAccessibleFields(scObj, scFieldMap, scExpectedFields);
            String formSoql = '';
            if(!allowedROFields.isEmpty())
            {
                formSoql = 'Select ';
                for(String roField: allowedROFields)
                {
                    formSoql += roField+', ';
                }
                formSoql = formSoql.substring(0, formSoql.length() - 2);
                
                //Not filtering using the ownerId as the records are used by all Admins. 
                formSoql += ' from Sales_Contract__c  sc where sc.Id = :plandocumentId';
                
                planDoc = Database.query(formSoql);//[SELECT Id, Name, Status__c, FirstName__c, LastName__c, Submit_Inquiry__c, User_ID__c FROM Sales_Contract__c sc where sc.Id = :plandocumentId];
                if(planDoc.size() > 0 && planDoc[0] == null)
                {   
                    return 'List has no rows'; 
                }
                
                if(planDoc.size() > 0)
                {
                    str=planDoc[0].FirstName__c;
                    firstName=planDoc[0].FirstName__c;
                    lastName=planDoc[0].LastName__c;

                    //Note: Whenever a new Custom field has to be updated, add it to the below Set.
                    //Reason: The size of the below set will be compared with the size of the filtered set 
                    Set<String> expectedUpdateableFields = new Set<String>{'Submit_Inquiry__c', 'Status__c'}; 
                    Set<String> filteredFields = dacu.getFilteredUpdateableFields(scFieldMap, expectedUpdateableFields);

                    //Allow to update the Template__c object only if the expected fields size is equal to filtered fileds
                    if(filteredFields.size() == expectedUpdateableFields.size())
                    {
                        planDoc[0].Submit_Inquiry__c = true; 
                        planDoc[0].Status__c = 'Inquiry Submitted';
                        if(UserInfo.getUserId() == planDoc[0].User_ID__c)    
                            update planDoc;
                    }
                }
                URL__c[] url = null;                    
                
                SObject sobj = new URL__c();
                Map<String, Schema.SObjectField> urlSObjectFieldMap = dacu.getFieldMap(sObj);
                String[] roFields = new String[]{'Id', 'Url_Value__c'}; 
                allowedROFields = dacu.getAccessibleFields(sObj, urlSObjectFieldMap, roFields);
                if(!allowedROFields.isEmpty())
                {
                    formSoql = 'Select ';
                    for(String roField: allowedROFields)
                    {
                        formSoql += roField+', ';
                    }
                    formSoql = formSoql.substring(0, formSoql.length() - 2);
                    formSoql += ' from Url__c ';
        
                    url =Database.query(formSoql);//[SELECT Id, Url_Value__c FROM URL__c];
                }
                
                
                Map<String, Schema.SObjectField> pdiFieldMap = dacu.getFieldMap(pdiObj);
    
                //Note: Whenever a new Custom field has to be created, add it to the below Set.
                //Reason: The size of the below set will be compared with the size of the filtered set 
                //Not checking "Plan_Document__c" as Plan DOcument Inquiry object has Master-Child relationship on this field
                Set<String> fieldsToBeCreatedList = new Set<String>{'Name', 'Reason__c', 'Description__c','InquiryCreated__c', 'InquiryStatus__c', 'Url__c'};
                
                //Filter the above list to get the fields that can be updated by the User. 
                Set<String> filteredList = dacu.getFilteredUpdateableFields(pdiFieldMap, fieldsToBeCreatedList);
                Plan_Document_Inquiry__c pdInquiry = new Plan_Document_Inquiry__c();
                if(filteredList.size() == fieldsToBeCreatedList.size())
                {       
                    pdInquiry.Plan_Document__c=plandocumentId;
                    pdInquiry.Reason__c=reason;
                    pdInquiry.Description__c=description;
                    pdInquiry.Name=sub;
                    pdInquiry.InquiryCreated__c=System.now();
                    pdInquiry.InquiryStatus__c='Pending';
                    if(url.size() > 0)
                    {
                        pdInquiry.Url__c=url[0].Url_Value__c;
                    }
                    str=''+'after assignment'+pdInquiry.Reason__c+pdInquiry.Description__c+pdInquiry.Name;
                    try
                    {
                        insert pdInquiry;
                    }
                    catch(DmlException dme)
                    {
                        return dme.getMessage();
                    }
                    if(approver != 'Administrator')
                    {
                        Approval.ProcessSubmitRequest request =new Approval.ProcessSubmitRequest();
                        request.setObjectId(pdInquiry.id);
                        Approval.ProcessResult result = Approval.process(request);
                    }
                }
            }        
        }
        return 'success';
    
    }
          
    static testMethod void testSubmitUtil() 
    {
        Sales_Contract__c sc = new Sales_Contract__c();
        Test.startTest();
        insert sc;
        String scId = sc.id;
        Sales_Contract__c[] scT = [select id from Sales_Contract__c where id = :sc.id];
        system.assertEquals(scId, scT[0].id);
        
        testVar1 = 'Published';
        createInquiryObject(scId,'test','test','test','Administrator');
        
      //  createInquiryObject(null,'test','test','test','Administrator');
        Test.stopTest();
    }
    
}